<?php
namespace app\Admin\controller;
use app\Admin\model\Goods as GoodsModel;
use app\admin\controller\Base;

class Goods extends Base
{
    public function index()
    {
        $goods=new GoodsModel();
        if(request()->isPost()){
            $sorts=input('post.');

            foreach ($sorts as $k => $v) {
                $goods->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新排序成功！',url('index'));
            return;
        }
        $goodsres=$goods->order('sort')->paginate(3);
        $this->assign('list',$goodsres);
        return view();
    }

    public function add()
    {   
        if(request()->isPost()){
            $data=input('post.');
            $data['addtime']=time();
            $validate = \think\Loader::validate('goods');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $goods=new GoodsModel;
            if($goods->save($data)){
                $this->success('添加商品成功',url('index'));
            }else{
                $this->error('添加商品失败！');
            }
            return;
        }
        return view();
    }

    public function edit(){
         if(request()->isPost()){
            $data=input('post.');
            /*var_dump($data);
            die;*/
            $validate = \think\Loader::validate('goods');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $goods = new GoodsModel;
            $save=$goods->update($data);
            if($save){
                $this->success('修改商品成功！',url('index'));
            }else{
                $this->error('修改商品失败！');
            }
            return;
        }
        $list=db('goods')->where(array('id'=>input('id')))->find();
        $this->assign('list',$list);
        return view();
    }

    public function onstatus()
    {
        $id = input('id');
        $goods = db('goods')->find($id);
        if ($goods['status'] == 1) {
            $data = [
                'id' => $id,
                'status' => 0,
            ];
            db('goods')->update($data);
            $this->redirect('index');
        } else {
            $data = [
                'id' => $id,
                'status' => 1,
            ];
            db('goods')->update($data);
            $this->redirect('index');
        }
    }

    public function ontype()
    {
        $id = input('id');
        $goods = db('goods')->find($id);
        if ($goods['limit'] == 1) {
            $data = [
                'id' => $id,
                'limit' => 0,
            ];
            db('goods')->update($data);
            $this->redirect('index');
        } else {
            $data = [
                'id' => $id,
                'limit' => 1,
            ];
            db('goods')->update($data);
            $this->redirect('index');
        }
    }

    public function del(){
        $id=input('id');
        if(db('goods')->delete(input('id'))){
            $this->success('删除商品成功！','index');
        }else{
            $this->error('删除商品失败！');
        }
        
    }



}
